package edu.vt.ece3574.ninjatag;

import java.util.HashMap;
import java.util.LinkedList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Bitmap;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;

import com.facebook.android.BaseRequestListener;
import com.facebook.android.FriendsGetProfilePics;
import com.facebook.android.Utility;

/**
 * As is, this is just for the Facebook implementation. But it
 * has the option to add dummy players.
 * @author Chris Dusold
 *
 */
public class FriendsList {
	private static HashMap<String,PlayerIf> friendsList_;
	private boolean fetching = false;
	
	public boolean available() {
		return !fetching;
	}
	
	public FriendsList() {
		friendsList_=new HashMap<String, PlayerIf>();
	}
	
	public FriendsList(boolean a) {
		Log.d("FL","AG");
		fetching = true;
		friendsList_=new HashMap<String, PlayerIf>();

		if (Utility.model == null) {
            Utility.model = new FriendsGetProfilePics();
        }
        //Utility.model.setListener(this);
		
        String query = "select name, current_location, uid, pic_square from user where uid in (select uid2 from friend where uid1=me()) AND is_app_user = 1 order by name";
        Bundle params = new Bundle();
        params.putString("method", "fql.query");
        params.putString("query", query);
        Utility.mAsyncRunner.request(null, params,
                new FriendsRequestListener());
		Log.d("FL","OW");
        
        if (a) {
        	put("dummy",1,new DummyPlayer());
        }
	}
	
	public void put(String type, long id, PlayerIf player) {
		friendsList_.put(""+type.charAt(0)+id, player);
	}
	
	public void put(String ninjaTagID, PlayerIf player) {
		friendsList_.put(ninjaTagID, player);
	}
	
	public PlayerIf get(String type, long id) {
		return friendsList_.get(""+type.charAt(0)+id);
	}
	
	public PlayerIf get(String ninjaTagID) {
		return friendsList_.get(ninjaTagID);
	}
	
	public boolean contains(String type, long id) {
		return friendsList_.containsKey(""+type.charAt(0)+id);
	}
	
	public boolean contains(String ninjaTagID) {
		return friendsList_.containsKey(ninjaTagID);
	}
	
	public int getCount() {
		synchronized (friendsList_) {
			return friendsList_.size();
		}
	}
	
	public LinkedList<String> getKeys() {
		return new LinkedList<String>(friendsList_.keySet());
	}
	
	public LinkedList<PlayerIf> getAll() {
		return new LinkedList<PlayerIf>(friendsList_.values());
	}

	public class DummyPlayer implements PlayerIf {
	
		@Override
		public String getName() {
			return "Samwise Gamgee";
		}
	
		@Override
		public String getScore() {
			return "9001";
		}
	
		@Override
		public Bitmap getPicture() {
			//ContextWrapper context;
			//return BitmapFactory.decodeResource(context.getResources(),R.drawable.ninja_tag);
			return null;
		}
	
		@Override
		public GPSIf getGPSDetails() {
			// TODO Replace with DummyGPS.
			return new GPSIf() {
				
				@Override
				public String getType() {
					return "Dummy";
				}
				
				@Override
				public long getTimeSinceLastCheck() {
					return -1;
				}
				
				@Override
				public String getSensitivity() {
					return "-1";
				}
				
				@Override
				public Location getLocation() {
					Location out = new Location("me");
					out.setLatitude(37.228675);
					out.setLongitude(-80.424578);
					return out;
				}

				@Override
				public void setLocation(Location location) {
					//Does nothing currently. Probably will never do anything.
					
				}
			};
		}
	
	}

	private class FriendsRequestListener extends BaseRequestListener {
	
	    @Override
	    public void onComplete(final String response, final Object state) {
			try {
				Log.d("FL","EE");
				JSONArray jsonArray = new JSONArray(response);
				for(int i = 0; i < jsonArray.length(); i++) {
					Log.d("FL",""+i);
		            JSONObject jsonObject = jsonArray.getJSONObject(i);
		            String loc = "";
		            try {
		            	JSONObject location = jsonObject.getJSONObject("current_location");
		            	loc = location.getString("city") + ", " + location.getString("state");
		            } catch (JSONException e){
		            	Log.d("Facebook JSON", "No location found for" + jsonObject.getString("name"));
		            }
					FacebookPlayer player = new FacebookPlayer(
							Long.valueOf((jsonArray.getJSONObject(i)).getString("uid")),
							jsonObject.getString("name"),
							null, //Utility.model.getImage(jsonObject.getString("uid"), jsonObject.getString("pic_square")),
		                    loc);
					put("facebook",Long.valueOf((jsonArray.getJSONObject(i)).getString("uid")),player);
				}
				fetching=false;
				
			} catch (Exception e) {		
				Log.w("FL","AG");
		        String query = "select name, current_location, uid, pic_square from user where uid in (select uid2 from friend where uid1=me()) AND is_app_user = 1 order by name";
		        Bundle params = new Bundle();
		        params.putString("method", "fql.query");
		        params.putString("query", query);
		        Utility.mAsyncRunner.request(null, params,
		                new FriendsRequestListener());
				e.printStackTrace();
			}
	    }
		
	}

}
